//
//  SetWeekViewController.swift
//  MyKebiao
//
//  Created by David on 2016/12/3.
//  Copyright © 2016年 David. All rights reserved.
//

import UIKit
@objc protocol SetViewDelegate {
    @objc optional func finishedSetWeek(week: Int)
}
class SetWeekViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    var delegate: SetViewDelegate?
    
    var picker: UIPickerView!

    var currentWeek = 1
    
    convenience init(currentWeek: Int){
        self.init()
        self.currentWeek = currentWeek
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        let navbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 64))
        navbar.barTintColor = themeColor
        navbar.tintColor = UIColor.white
        navbar.barStyle = .black
        let navitem = UINavigationItem(title: "设置当前周")
        navitem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(SetWeekViewController.close))
        navbar.pushItem(navitem, animated: true)
        view.addSubview(navbar)
        
        picker = UIPickerView(frame: CGRect(x: 0, y: 64, width: view.bounds.width, height: view.bounds.height/2))
        picker.delegate = self
        picker.dataSource = self
        view.addSubview(picker)
        
        picker.selectRow(currentWeek-1, inComponent: 0, animated: false)
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 25
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return "第\(row + 1)周"
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        
    }
    
    func close(){
        let selectedWeek = picker.selectedRow(inComponent: 0) + 1
        if selectedWeek != currentWeek {
            delegate?.finishedSetWeek?(week: selectedWeek)
            //推算第一周的时间，保存在本地
            let format = DateFormatter()
            format.dateFormat = "yyyy-MM-dd"
            let todayWeek = Calendar.current.component(.weekOfYear, from: Date())
            let firstWeek = (54 + todayWeek - selectedWeek + 1)%54
            var setting = NSMutableDictionary(contentsOfFile: "\(KB_DOCUMENT_PATH)/setting.plist")
            if setting == nil{
                setting = NSMutableDictionary()
            }
            setting?.setValue(firstWeek, forKey: "firstWeek")
            setting?.write(toFile: "\(KB_DOCUMENT_PATH)/setting.plist", atomically: true)
        }
        dismiss(animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
